
    var keyPrefix = 'FlickrGroupPoolTagChecker.';

    GM_addStyle = function(css) {
        var head = document.getElementsByTagName('head')[0], style = document.createElement('style');
        if (!head) {return}
        style.type = 'text/css';
        try {style.innerHTML = css} catch(x) {style.innerText = css}
        head.appendChild(style);
    }

    GM_log = function (message) {
        console.info(message);
    }

    GM_getValue = function(key, defValue) {
        var retval = window.localStorage.getItem(keyPrefix + key);
        if (!$chk(retval)) {
            return defValue;
        }
        return retval;
    }

    GM_setValue = function(key, value) {
        try {
            window.localStorage.setItem(keyPrefix + key, value);
        } catch (e) {
            GM_log("error setting value: " + e);
        }
    }

    GM_deleteValue = function(key) {
        try {
            window.localStorage.removeItem(keyPrefix + key);
        } catch (e) {
            GM_log("error removing value: " + e);
        }
    }

    GM_listValues = function() {
        var list = [];
        var reKey = new RegExp("^" + keyPrefix);
        for (var i = 0, il = window.localStorage.length; i < il; i++) {
            // only use the script's own keys
            var key = window.localStorage.key(i);
            if (key.match(reKey)) {
                list.push(key.replace(keyPrefix, ''));
            }
        }
        return list;
    }

    GM_getObject = function (key) {
        var value = GM_getValue(key);
        if ($chk(value)) {
            try {
                return {
                    value: JSON.parse(value),
                    key_present: true
                };

            } catch (e) {
                var error = "error parsing (" + e + ") object '" + key + "': " + value;
                GM_log(error);
                return {
                    value: null,
                    key_present: true,
                    error: error
                };
            }
        }
        return {
            value: null,
            key_present: false
        };
    }

    GM_storeObject = function (key, value) {
        GM_setValue(key, JSON.stringify(value));
    }

var getJSVariable = function (regex) {
        // Thanks to Vispillo for this compact code
        var retval;
        $$('script').each( function (script) {
            if (retval != undefined) {
                return;
            }
            var html = script.innerHTML;
            try {
                retval = html.match(regex)[1];
            } catch (e) {
            }
        });
        return retval;
    }

    GM_getLoggedInUser = function () {
    	var username = getJSVariable(/[\"\']user[\"\'][: ]+{[^}]*[\"\']name[\"\'][: ]+[\"\']([^\"\']*)[\"\']/);
	if (!$chk(username)) {
		GM_log("unable to find user name in JSON data");
	        try {
        	    username = $('head-status').getElement('a.ywa-track').get('html');
	        } catch (e) {
        	    GM_log("unable to retrieve user");
	        }
	}
	return username;
    }

    GM_getMagisterLudi = function () {
        var reMatch = /\"?api_key\"?[ :]+[\'\"]([^\'\"]+)[\'\"]/;
	return getJSVariable(reMatch);
    }

    GM_getUserNsid = function () {
    	var reMatch = /\"nsid\"[ :]+[\'\"]([^\'\"]+)[\'\"]/;
	return getJSVariable(reMatch);
    }

    GM_getGroupId = function () {
    	var reMatch = /\"id\"[ :]+[\'\"]([^\'\"]+@\w\d{2})[\'\"]/;
	return getJSVariable(reMatch);
    }

    GM_getAuthHash = function () {
        var reMatch = /\"?auth_hash\"?[ :]+[\'\"]([^\'\"]+)[\'\"]/;
	return getJSVariable(reMatch);
    }

    GM_getAuthToken = function () {
        var reMatch = /global_auth_token[ =]+\'([^\']+)\'/;
        var retval = getJSVariable(reMatch);
	if (!$chk(retval)) {
            reMatch = /\"auth_token\"[ :]+[\'\"]([^\'\"]+)[\'\"]/;
	    retval = getJSVariable(reMatch);
	}
        return retval;
    }
